package net.entelijan.cobean.bind.impl;

import net.entelijan.cobean.bind.impl.BoundPropBinding;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
import org.junit.Test;

public class PathDoubleAttrTestCase {

    private static Log log = LogFactory.getLog(PathDoubleAttrTestCase.class);

    @Test
    public void testBinding() { 
        InvisibleCobean cb1 = new InvisibleCobean();
        InvisibleCobean cb2 = new InvisibleCobean();
        NestedDoubleAttrBean m = new NestedDoubleAttrBean("11", "12", new NestedDoubleAttrBean(
                "21", "22", new NestedDoubleAttrBean("31", "32", null)));
        {
            BoundPropBinding b = new BoundPropBinding();
            b.setCompProperty("value");
            b.setModelPath("beanVal.beanVal.strVal1");
            log.debug("----(0)- b.bind(m, cb1); ---------------------");
            b.bind(m, cb1);
            log.debug("----(1)- b.bind(m, cb1); ---------------------");
        }
        {
            BoundPropBinding b = new BoundPropBinding();
            b.setCompProperty("value");
            b.setModelPath("beanVal.beanVal.strVal2");
            log.debug("----(0)- b.bind(m, cb2); ---------------------");
            b.bind(m, cb2);
            log.debug("----(1)- b.bind(m, cb2); ---------------------");
        }
        Assert.assertEquals("31", cb1.getValue());
        Assert.assertEquals("32", cb2.getValue());
    }

    @Test
    public void testBinding01() {
        InvisibleCobean cb1 = new InvisibleCobean();
        InvisibleCobean cb2 = new InvisibleCobean();
        NestedDoubleAttrBean m = new NestedDoubleAttrBean("11", "12", new NestedDoubleAttrBean(
                "21", "22", new NestedDoubleAttrBean("31", "32", null)));
        {
            BoundPropBinding b = new BoundPropBinding();
            b.setCompProperty("value");
            b.setModelPath("beanVal.beanVal.strVal1");
            log.debug("----(0)- b.bind(m, cb1); ---------------------");
            b.bind(m, cb1);
            log.debug("----(1)- b.bind(m, cb1); ---------------------");
        }
        {
            BoundPropBinding b = new BoundPropBinding();
            b.setCompProperty("value");
            b.setModelPath("beanVal.beanVal.strVal2");
            log.debug("----(0)- b.bind(m, cb2); ---------------------");
            b.bind(m, cb2);
            log.debug("----(1)- b.bind(m, cb2); ---------------------");
        }
        m.getBeanVal().getBeanVal().setStrVal1("HALLO");
        m.getBeanVal().getBeanVal().setStrVal2("WIE");
        Assert.assertEquals("HALLO", cb1.getValue());
        Assert.assertEquals("WIE", cb2.getValue());
    }

    @Test
    public void testRebinding() {
        InvisibleCobean cb1 = new InvisibleCobean();
        InvisibleCobean cb2 = new InvisibleCobean();
        NestedDoubleAttrBean m;
        {
            final NestedDoubleAttrBean i1 = new NestedDoubleAttrBean("31", "32", null);
            final NestedDoubleAttrBean i0 = new NestedDoubleAttrBean("21", "22", i1);
            m = new NestedDoubleAttrBean("11", "12", i0);
        }
        {
            BoundPropBinding b = new BoundPropBinding();
            b.setCompProperty("value");
            b.setModelPath("beanVal.beanVal.strVal1");
            log.debug("----(0)- m = b.bind(m, cb1); ---------------------");
            m = b.bind(m, cb1);
            log.debug("----(1)- m = b.bind(m, cb1); ---------------------");
        }
        {
            BoundPropBinding b = new BoundPropBinding();
            b.setCompProperty("value");
            b.setModelPath("beanVal.beanVal.strVal2");
            log.debug("----(0)- m = b.bind(m, cb2); ---------------------");
            m = b.bind(m, cb2);
            log.debug("----(1)- m = b.bind(m, cb2); ---------------------");
        }
        {
            final NestedDoubleAttrBean i1 = new NestedDoubleAttrBean("51", "52", null);
            log
                    .debug("----(0)- m.setBeanVal(new NestedDoubleAttrBean(\"41\", \"42\", i1)); ---------------------");
            m.setBeanVal(new NestedDoubleAttrBean("41", "42", i1));
            log
                    .debug("----(1)- m.setBeanVal(new NestedDoubleAttrBean(\"41\", \"42\", i1)); ---------------------");
        }
        
        Assert.assertEquals("51", cb1.getValue());
        Assert.assertEquals("52", cb2.getValue());
    }

}
